package cn.zlp.user.controller;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.util.AjaxResult;
import cn.zlp.user.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 验证码控制接口
 * @program: pethome
 * @author: Mr.Zhang
 * @create: 2021-12-07 19:19
 **/
@RestController
@RequestMapping("/verifycode")
@Api(tags = "手机短信验证码接口",description = "手机验证码接口详细描述")
public class VerifycodeController {

    @Autowired
    private IUserService userService;

    /**
     * 手机注册时发送短信验证码
     * @param map
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody Map<String,String> map){
        try {
            // 取出电话号码
            String phone = map.get("phone");
            // 当前手机发送短信验证码
            userService.smsCode(phone);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("验证错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 微信绑定时发送手机验证码
     * @param map
     * @return
     */
    @PostMapping("/binderCode")
    public AjaxResult sendSmsBinderCode(@RequestBody Map<String,String> map){
        try {
            // 取出电话号码
            String phone = map.get("phone");
            // 当前手机发送短信验证码
            userService.smsBinderCode(phone);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("验证错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 手机登录时发送手机验证码
     * @param map
     * @return
     */
    @PostMapping("/loginCode")
    public AjaxResult sendLoginCode(@RequestBody Map<String,String> map){
        try {
            // 取出电话号码
            String phone = map.get("phone");
            // 当前手机发送短信验证码
            userService.sendLoginCode(phone);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("验证错误," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

}
